﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseFunction;

namespace YY.Web.MyAgent
{
    public partial class MySalesPerformanceNoBills : System.Web.UI.Page
    {
        int page = 0;
        int rows = 0;
        private string strWhere = "";
        public double sum_accout = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                StrWhere();
            }
        }
        /// <summary>
        /// 初始化操作
        /// </summary>
        private void InitData(string strWhere)
        {
            page = AspNetPager1.CurrentPageIndex; //当前页码
            rows = AspNetPager1.PageSize; //每页显示多少条
            string tabName = "dbo.PackageInfo T1 INNER JOIN dbo.Dealrecordlist T2 ON(T1.id=T2.packageguid) INNER JOIN dbo.tb_UserInfor T3 ON(T2.userguid=T3.FinancialAccount)";
            string columns = "T2.dealtime as sales_time,T3.UserName as user_name,T1.name as type_of_packages,T1.price as sales_bonus";
            string orderBy = "T2.dealtime desc";
            DataTable dt = MsSql2008.ExecSqlPagerPROCEDURE(columns, tabName, strWhere, orderBy, page, rows).Tables[2];
            rpt_sales_performance.DataSource = dt;
            rpt_sales_performance.DataBind();
            /*当前月份*/
            ltl_month.Text = DateTime.Now.Month + "";
            /*计算时间段统计*/
            ltl_sum.Text = MsSql2008.Query(string.Format("SELECT COUNT(*) FROM dbo.PackageInfo T1 INNER JOIN dbo.Dealrecordlist T2 ON(T1.id=T2.packageguid) INNER JOIN dbo.tb_UserInfor T3 ON(T2.userguid=T3.FinancialAccount) WHERE {0}", strWhere)).Tables[0].Rows[0][0] + "";
            /*A套餐*/
            ltl_A.Text = MsSql2008.Query(string.Format("SELECT COUNT(*) FROM dbo.PackageInfo T1 INNER JOIN dbo.Dealrecordlist T2 ON(T1.id=T2.packageguid) INNER JOIN dbo.tb_UserInfor T3 ON(T2.userguid=T3.FinancialAccount) WHERE {0} AND T1.name='A'", strWhere)).Tables[0].Rows[0][0] + "";
            /*B套餐*/
            ltl_B.Text = MsSql2008.Query(string.Format("SELECT COUNT(*) FROM dbo.PackageInfo T1 INNER JOIN dbo.Dealrecordlist T2 ON(T1.id=T2.packageguid) INNER JOIN dbo.tb_UserInfor T3 ON(T2.userguid=T3.FinancialAccount) WHERE {0} AND T1.name='B'", strWhere)).Tables[0].Rows[0][0] + "";
            /*C套餐*/
            ltl_C.Text = MsSql2008.Query(string.Format("SELECT COUNT(*) FROM dbo.PackageInfo T1 INNER JOIN dbo.Dealrecordlist T2 ON(T1.id=T2.packageguid) INNER JOIN dbo.tb_UserInfor T3 ON(T2.userguid=T3.FinancialAccount) WHERE {0} AND T1.name='C'", strWhere)).Tables[0].Rows[0][0] + "";
            /*总销售额*/
            var dt_sum = MsSql2008.Query(string.Format("SELECT SUM(CONVERT(DECIMAL(18,1),T1.price) ) FROM dbo.PackageInfo T1 INNER JOIN dbo.Dealrecordlist T2 ON(T1.id=T2.packageguid) INNER JOIN dbo.tb_UserInfor T3 ON(T2.userguid=T3.FinancialAccount) WHERE {0}", strWhere));
            if (dt_sum.Tables[0].Rows[0][0]==DBNull.Value)
            {
                sum_accout = 0;
                ltl_account.Text = ":0元";
            }
            else
            {
                sum_accout = Convert.ToDouble(dt_sum.Tables[0].Rows[0][0]);
                ltl_account.Text = sum_accout + "元";
                ltl_sum_accout.Text = sum_accout + "元";
                if (sum_accout >= 10 && sum_accout <= 5000000)
                {
                    ltl_point.Text = "0.04";
                    ltl_gain.Text = sum_accout * 0.04 + "";
                }
                if (5000000 < sum_accout && sum_accout <= 20000000)
                {
                    ltl_point.Text = "0.06";
                    ltl_gain.Text = sum_accout * 0.06 + "";
                }
                if (20000000 < sum_accout && sum_accout <= 50000000)
                {
                    ltl_point.Text = "0.08";
                    ltl_gain.Text = sum_accout * 0.08 + "";
                }
                if (50000000 < sum_accout && sum_accout <= 100000000)
                {
                    ltl_point.Text = "0.1";
                    ltl_gain.Text = sum_accout * 0.1 + "";
                }
            }
        }

        private void StrWhere()
        {
            string start = DateTime.Now.AddDays(-15).ToShortDateString() + "";
            string end = DateTime.Now.AddDays(1).ToShortDateString() + "";
            strWhere = string.Format("T2.dealtime between '{0}' and '{1}'", start, end);
            InitData(strWhere);
        }

        #region  分页操作 buttom
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            StrWhere();

        }
        #endregion
    }
}